package ks.client.controllers.requests;

import java.util.Collection;

import javax.swing.JFrame;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import ks.client.UserContext;
import ks.client.gui.GMGUI;
import ks.client.lobby.Lobby;
import ks.client.lobby.RoomGUI;
import ks.framework.common.Message;
import ks.framework.interfaces.IProcessMessage;
import ks.managers.table.Table;
import ks.managers.table.TableManager;

/**
 * Parameters for this controller class are received from a server side controller 
 * that is sent a command from the JoinTableClick Controller.
 * 
 * @author szutshi
 * @author Christopher Page
 */
public class JoinedController implements IProcessMessage{
	Lobby lobby;

public JoinedController(Lobby lobby){ 
	this.lobby = lobby;
}
	public boolean process(Message m){
		
		Node joined = m.contents();
		
		if (joined == null)
			return false;
		if (!joined.getNodeName().equalsIgnoreCase("joined"))
			return false;
		
		NamedNodeMap nnp = joined.getAttributes();
		
		Node tableNode = nnp.getNamedItem("table");
		Node playerNode = nnp.getNamedItem("player");
		
		if (tableNode == null)
			return false;
		if (playerNode == null)
			return false;
		
		int tableNum = Integer.parseInt(tableNode.getNodeValue());
		String player = playerNode.getNodeValue();
		
		UserContext uc = lobby.getUserContext();
		TableManager tm = uc.getTableManager();
		Table t = tm.getTable(tableNum);
		String mod = t.getModerator();
		if (t == null)
			return false;
		
		t.addPlayer(player);
		String list = t.getPlayers().toString();
		
	
		GMGUI gui = lobby.getGMGUI();
		if (gui != null) {
			gui.updateUserList();
		}
		
		return true;
		
	}
}
	
